package com.zhihui.online.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhihui.online.dto.student.StudentPageDTO;
import com.zhihui.online.dto.student.StudentUpdateDTO;
import com.zhihui.online.entity.Student;
import com.zhihui.online.exception.MyCustomerException;
import com.zhihui.online.service.StudentService;
import com.zhihui.online.utils.MD5Helper;
import com.zhihui.online.utils.Result;
import com.zhihui.online.utils.ResultCodeEnum;
import com.zhihui.online.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author 程序员阿良,21岁纯情后爸,懒得想名字
 * @since 2024-08-01
 */
@RestController
@Api(tags = "学生管理控制器")
@RequestMapping("/online/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @ApiOperation("分页列表")
    @GetMapping("/{page}/{limit}")
    public Result listPageStudent(@PathVariable Long page,
                                  @PathVariable Long limit,
                                  StudentPageDTO studentPageDTO
    ){
        IPage<StudentVO> ipage =new Page<>(page,limit);
        ipage=this.studentService.selectPage(ipage,studentPageDTO);
        return Result.ok(ipage);
    }

    @PostMapping("/addStudent")
    @ApiOperation("添加学生")
    public Result addStudent(@RequestBody Student student){
        //通过MD5 工具类
     String passwordWithMD5= MD5Helper.encrypt(student.getPassword());
     student.setPassword(passwordWithMD5);
     boolean b=this.studentService.save(student);
     if (b){
        return Result.ok();
     }else{
         return Result.fail();
     }
    }

    @ApiOperation("根据id获取学生")
    @GetMapping("/findStudentById/{id}")
    public Result findStudentById(@PathVariable Long id){
        Student student =this.studentService.getById(id);
        if (student == null) {
            throw new MyCustomerException(ResultCodeEnum.USER_NOT_EXIST);
        }
        StudentVO studentVo = this.studentService.getVo(student);
        return Result.ok(studentVo);
    }


    @ApiOperation("修改学生信息")
    @PostMapping("/updateStudent")
    public Result<Object> updateStudent(@RequestBody StudentUpdateDTO studentupdateDTO){
        if (studentupdateDTO == null || studentupdateDTO.getId() == null) {
            throw new MyCustomerException(ResultCodeEnum.DATA_ERROR);
        }
        //转换为实体类
        Student student = new Student();
        BeanUtils.copyProperties(studentupdateDTO, student);
        boolean b=this.studentService.updateById(student);
        return b ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据id去删除用户")
    @DeleteMapping("/deletById/{id}")
    public Result deletStudentById(@PathVariable Long id){
    boolean b=this.studentService.removeById    (id);
    if (b){
        return Result.ok();
    }else {
        return Result.fail();
    }

    }
    @ApiOperation("批量删除")
    @DeleteMapping("/deletByIds")
    public Result deletStudentByIds(@RequestBody List<Long> ids){
        boolean b=this.studentService.removeByIds(ids);
        if (b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    @ApiOperation("更改学生状态")
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id,@PathVariable Integer status){
    this.studentService.updateStatus(id,status);
    return Result.ok();
    }

}

